<?php

namespace app\admin\model\zhongtai;

use think\Model;

class ShoproGoodsSku extends Model
{
// 表名
    protected $table = 'shopro_goods_sku';
    // 设置当前模型的数据库连接
    protected $connection = 'db_zhongtai';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    /**
     * 获取最新的更新时间
     */
    public function getUpTime($manage_id)
    {
        $updatetime = $this->where('manage_id', $manage_id)->order("zt_time desc")->value("zt_time");
        return $updatetime ?? 0;
    }

    public function upData($data, $manage_id)
    {
        if ($data) {
            $con = count($data);
            foreach ($data as $k => $v) {
                echo $k . PHP_EOL;
                if (($con - 1) == $k) {
                    $zttime = date('Y-m-d H:i:s',strtotime($v['zt_time'])+1);
                }else{
                    $zttime = $v['zt_time'];
                }
                $in_data = [
                    "manage_id" => $manage_id,
                    "goods_sku_id" => $v['id'],
                    "name" => $v['name'],
                    "pid" => $v['pid'],
                    "goods_id" => $v['goods_id'],
                    "weigh" => $v['weigh'],
                    "zt_time" => $zttime,
                ];
                $user_data = $this->where([
                    'goods_sku_id' =>  $v['id'],
                    'manage_id' => $manage_id,
                ])->find();
                if ($user_data) {
                    $this->where([
                        'goods_sku_id' =>  $v['id'],
                        'manage_id' => $manage_id,
                    ])->update($in_data);
                } else {
                    $this->insert($in_data);
                }
            }
        }
        echo "shopro_goods_sku表更新完成";
    }
}
